package fr.utbm.gi.vi51.project.formations;

import java.util.ArrayList;
import java.util.Collection;

import javax.vecmath.Point2d;

public class SpreadFormation  extends FormationBody {

	private static final int INTER_SLOT_SPACE = 25;

	public SpreadFormation (FormationOrientation orientation) {
		super();
		this.orientation = orientation;
	}

	@Override
	public Collection<FormationSlot> scaleAt(int numberOfSlots, Point2d Position) {
		ArrayList<FormationSlot> formationSlots = new ArrayList<>();
		if (numberOfSlots > 0) {
			FormationSlot leaderSlot = new FormationSlot(this);
			formationSlots.add(leaderSlot);
			// Remember the number of added slots. Each time a slot needs to be added, verify that addedSlotCount <
			// numberOfSlots. If it isn't, stop the method.
			int addedSlotCount = 1;
			// Count the number of spreeds
		
int i=0;
			switch (this.orientation) {
			case SOUTH:
				while(addedSlotCount < numberOfSlots){
				i=i+1;
					for(int j=i;j>=-i;j=j-2)
					{
				if(j==i||j==-i)
				{
					
	            if (addedSlotCount < numberOfSlots&&i%2==0) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount, 0, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					for(int k=2-i%2;k<=i;k=k+2){
					if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,k*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,-k*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					
					}
					
				}
				else{
					
	          if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,i*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,-i*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}	
					
					
				}
						
						
						
					}
					
					
					
				}
			
				break;
			case NORTH:
				while(addedSlotCount < numberOfSlots){
					i=i+1;
					
					for(int j=-i;j<=i;j=j+2)
					{
				if(j==i||j==-i)
				{
					
	            if (addedSlotCount < numberOfSlots&&i%2==0) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount, 0, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					for(int k=2-i%2;k<=i;k=k+2){
					if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,k*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,-k*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					
					}
					
				}
				else{
					
	          if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,i*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,-i*INTER_SLOT_SPACE, j*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}	
					
					
				}
						
						
						
					}
					
					
					
				}
			
				break;
			case WEST:
				while(addedSlotCount < numberOfSlots){
					i=i+1;
					
					for(int j=-i;j<=i;j=j+2)
					{
				if(j==i||j==-i)
				{
					
	            if (addedSlotCount < numberOfSlots&&i%2==0) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount, j*INTER_SLOT_SPACE,0,  0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					for(int k=2-i%2;k<=i;k=k+2){
					if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, k*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, -k*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					
					}
					
				}
				else{
					
	          if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, i*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, -i*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}	
					
					
				}
						
						
						
					}
					
					
					
				}
			
				break;
			default:
				while(addedSlotCount < numberOfSlots){
					i=i+1;
					
					for(int j=i;j>=-i;j=j-2)
					{
				if(j==i||j==-i)
				{
					
	            if (addedSlotCount < numberOfSlots&&i%2==0) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount, j*INTER_SLOT_SPACE,0,  0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					for(int k=2-i%2;k<=i;k=k+2){
					if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, k*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, -k*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
					
					}
					
				}
				else{
					
	          if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, i*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}
	                   if (addedSlotCount < numberOfSlots) {
						
						FormationSlot newSlot = new FormationSlot(leaderSlot, this, addedSlotCount,j*INTER_SLOT_SPACE, -i*INTER_SLOT_SPACE, 0);
						formationSlots.add(newSlot);
						addedSlotCount++;	
						
					}	
					
					
				}
						
						
						
					}
					
					
					
				}
			
				break;
			}
		}
		return formationSlots;
	}



}